<?php


namespace app\admin\controller;


use app\common\controller\admin\Base;
use app\admin\model\Category;
use app\admin\model\PayConfig;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\admin\model\AppBase;

class Apppayconfig extends Base
{

    protected $noNeedLogin = [];

    protected $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AppPayConfig();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $params = $this->request->post("");
            $where = [
//                'switch' => 1,
                'is_del' => 0
            ];
            $appBaseList = AppBase::field('id,title,switch')->where($where)->select()->toArray();
            $appBaseList = array_column($appBaseList, null, 'id');
            $appBaseList['0'] = [
                'id' => 0,
                'title' => "默认",
                'switch' => 1
            ];
            $appPayConfigList = $this->model
                ->where('country_config_id', $params['country_config_id'])
                ->select()
                ->toArray();
            if (!empty($appPayConfigList)) {
                $appPayConfigList = array_column($appPayConfigList, null, 'app_base_id');
            }


            $payConfigList = PayConfig::select()->toArray();
            $payConfigList = array_column($payConfigList, null, 'id');
            $list = [];
            foreach ($appBaseList as $value) {
                if (empty($appPayConfigList[$value['id']])) {
                    $list[] = [
                        'app_base_id' => $value['id'],
                        'app_base_switch' => $value['switch'],
                        'app_base_name' => $value['title'],
                        'loan_vip_pay_config_id' => empty($appPayConfigList[0]['loan_vip_pay_config_id']) ? 0 : $appPayConfigList[0]['loan_vip_pay_config_id'],
                        'loan_vip_pay_config_name' => empty($appPayConfigList[0]['loan_vip_pay_config_id']) ? '' : $payConfigList[$appPayConfigList[0]['loan_vip_pay_config_id']]['title'],
                        'other_pay_config_id' => empty($appPayConfigList[0]['loan_vip_pay_config_id']) ? '' : $appPayConfigList[0]['other_pay_config_id'],
                        'other_pay_config_name' => empty($appPayConfigList[0]['loan_vip_pay_config_id']) ? '' : $payConfigList[$appPayConfigList[0]['other_pay_config_id']]['title'],
                    ];
                } else {
                    $list[] = [
                        'app_base_id' => $value['id'],
                        'app_base_name' => $value['title'],
                        'app_base_switch' => $value['switch'],
                        'loan_vip_pay_config_id' => $appPayConfigList[$value['id']]['loan_vip_pay_config_id'],
                        'loan_vip_pay_config_name' => $payConfigList[$appPayConfigList[$value['id']]['loan_vip_pay_config_id']]['title'],
                        'other_pay_config_id' => $appPayConfigList[$value['id']]['other_pay_config_id'],
                        'other_pay_config_name' => $payConfigList[$appPayConfigList[$value['id']]['other_pay_config_id']]['title'],
                    ];
                }
            }
            krsort($list);
            $this->success('列表', '', ['list' => array_values($list)]);
        }
    }

    /**
     * 新增或者编辑
     */
    public function insertOrUpdate()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = $name . '.insertOrUpdate';
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $params['admin_id'] = $this->admin['id'];
                    $row = $this->model->where('app_base_id', $params['app_base_id'])->where('country_config_id', $params['country_config_id'])->find();
                    //如果查无此数据则新增有则编辑
                    if (empty($row)) {
                        $result = $this->model->allowField(true)->save($params);
                    } else {
                        $result = $row->allowField(true)->isUpdate(true)->save($params);
                    }
                    //判断处理 如果修改的是默认配置 则其他配置也对应改为默认配置
                    if ($params['app_base_id'] == 0) {
                        $updateData = [
                            'loan_vip_pay_config_id' => $params['loan_vip_pay_config_id'],
                            'other_pay_config_id' => $params['other_pay_config_id'],
                        ];
                        $this->model->update($updateData, ['country_config_id' => $params['country_config_id']]);
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
            $this->error('参数不能为空！');
        }
    }

}